package com.greenDao.activity.check;

import java.util.List;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.greenDao.activity.R;
import com.greenDao.activity.tool.DatabaseUtils;
import com.greenDao.activity.tool.GuiUtils;
import com.greenDao.activity.tool.Marker;
import com.greenDao.activity.tool.Utils;
import com.greenDao.activity.tool.button.BackButton;
import com.greenDao.activity.tool.button.event.ModifyEventButton;
import com.greenDao.activity.tool.button.event.RemoveEventButton;
import com.greenDao.pojo.Event;

public class CheckEventActivity extends AbstractCheckActivity {

	private static SparseIntArray positionToId = new SparseIntArray();
	private static Marker marker = new Marker();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_check_event);
		setUpActionBar("Events: ");

		GuiUtils.hookButton(this, R.id.fragmBack, new BackButton(this));
	}

	void updateActionBar(ActionBar actionBar) {
		List<Event> events = DatabaseUtils.getAllEvents();
		String[] titles = Utils.extractDataFromEvents(true, events, positionToId);
		marker.updateLimit(titles.length);
		actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(actionBar.getThemedContext(),
				android.R.layout.simple_list_item_1, android.R.id.text1, titles), this);
		actionBar.setSelectedNavigationItem(marker.getMarker());
	}

	@Override
	public boolean onNavigationItemSelected(int position, long id) {

		Event event = DatabaseUtils.findEventById(Long.valueOf(positionToId.get(position)));
		EventSectionFragment fragment = new EventSectionFragment();
		fragment.setEvent(event);
		fragment.setCheckEventActivity(this);
		marker.setMarker(position);
		getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();

		return true;
	}

	public static class EventSectionFragment extends Fragment {

		private Event event;
		private CheckEventActivity checkEventActivity;

		public EventSectionFragment() {
		}

		public void setEvent(Event event) {
			this.event = event;
		}

		public void setCheckEventActivity(CheckEventActivity checkEventActivity) {
			this.checkEventActivity = checkEventActivity;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_check_event, container, false);

			GuiUtils.setTextView(rootView, R.id.fragmName, event.getName());
			GuiUtils.setTextView(rootView, R.id.fragmMovieTitle, event.getMovie().getTitle());
			GuiUtils.setTextView(rootView, R.id.fragmMovieDirector, event.getMovie().getDirector());
			GuiUtils.setTextView(rootView, R.id.fragmMovieYear, event.getMovie().getYear());
			GuiUtils.setTextView(rootView, R.id.fragmTime, event.getTime());
			GuiUtils.setTextView(rootView, R.id.fragmComment, event.getComment());

			GuiUtils.hookButton(rootView, R.id.fragmModify, new ModifyEventButton(this, event));
			GuiUtils.hookButton(rootView, R.id.fragmRemove, new RemoveEventButton(checkEventActivity, event));

			return rootView;
		}
	}
}
